//泰山派默认没开启mqueue
#include <fcntl.h>
#include <sys/stat.h>
#include <mqueue.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
void sys_error(const char *msg) {
    perror(msg);
    exit(EXIT_FAILURE);
}
int main(int argc, char const *argv[])
{
    struct mq_attr attr;
    attr.mq_maxmsg = 10;
    attr.mq_msgsize = 100;
    mqd_t mq1 = mq_open("/mq1", O_CREAT | O_RDWR, 0644, attr);
    if(mq1 == -1)
        sys_error("mq open failed");
    pid_t pid = fork();
    if(pid == 0){
        char read_buf[100];
        for (int i = 0; i < 10; i++)
        {
            memset(read_buf, 0, 100);
            if (mq_receive(mq1, read_buf, sizeof(read_buf), NULL) == -1)
            {
                sys_error("receive failed");
            }
            else{
            printf("received: %s\n", read_buf);
            }
        }
    }
    else{
        char msg[100];
        for (int i = 0; i < 10; i++)
        {
            sprintf(msg, "hello%d", i);
            if(mq_send(mq1,msg,100,0)==-1){
                sys_error("send failed");
            }else{
                printf("send:%s\n",msg);
            }
            usleep(500);
        }
    }

    close(mq1);
    if(pid > 0)
        mq_unlink("/mq1");
    return 0;
}
